package com.yanqu.road.server.cmd.activity.fivehero;

import com.yanqu.road.entity.activity.fivehero.FiveHeroConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FiveHeroProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.fivehero.FiveHeroModule;

import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_FIVEHERO_NOTIFY_SHOP_FROM_CROSS, desc = "")
public class CrossFiveHeroNotifyShopCmd extends AbstractCommand {

    private FiveHeroProto.FiveHeroNotifyGameServerShop reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = FiveHeroProto.FiveHeroNotifyGameServerShop.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        FiveHeroConfig config = FiveHeroMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if (reqMsg.getStat() == 0) {
            int mallId = player.getModule(FiveHeroModule.class).notifyShopEvent();
            player.getModule(FiveHeroModule.class).giveUpShopEvent();
            if(mallId > 0){
                FiveHeroProto.CrossFiveHeroGiftLogMsg.Builder logMsg = reqMsg.getLog().toBuilder();
                logMsg.setMallId(mallId);
//                player.sendChannelPacket(CrossProtocol.C2_CROSS_FIVE_LOG_RMB_GIFT, logMsg);
                int groupId = GameUnionActivityGroupMgr.getGroupId(reqMsg.getLog().getActivityId(),player.getUserId());
                GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_FIVE_LOG_RMB_GIFT,
                        logMsg,player.getUserId(),reqMsg.getLog().getActivityId(),groupId);
            }
        } else {
            player.getModule(FiveHeroModule.class).giveUpShopEvent();
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }
    }
}
